I got the following error while uploading a file using FileUpload control. I solved by getting the file contents in bytes so you can assign to it.
public void Compress()
{
string fileWithExt = Path.GetFileName(FileUpload1.FileName);
string zipFile = fileWithExt + ".zip";
string zipFilePath = Path.Combine(Server.MapPath("~/Uploads"), zipFile);
using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
zipstream.Write(data, 0, data.Length);
zipstream.Flush();
}
}
Exception message:
Solution:
You just replace the above code with the highlighted code.
byte[] fileBytes = FileUpload1.FileBytes;
using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = fileBytes;
zipstream.Write(data, 0, data.Length);
zipstream.Flush(); }
Post your comments / questions
Recent Article
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
Related Article